var pageii = null;
var grid = null;
$(function() {
	grid = lyGrid({
		pagId : 'paging',
		l_column : [ {
			colkey : "id",
			name : "id",
			isSort:true,
		}, {
			colkey : "proType",
			name : "促销类型",
			renderData:function(rowindex,data,rowdata,colkeyn){
				console.log(rowdata.id);
				 if('0' == data){
					 return "订单满减";
				 }else if('1' == data){
					 return "商品满减";
				 }else if('2' == data){
					 return "商品满赠";
				 }else if('3' == data){
					 return "商品打折";
				 }
			}
		}, {
			colkey : "proTitle",
			name : "促销名称",
			isSort:true,
		}, {
			colkey : "proStart",
			name : "活动开始时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd");
			}
		}, {
			colkey : "proEnd",
			name : "活动结束时间",
			renderData : function(rowindex,data, rowdata, column) {
				return new Date(data).format("yyyy-MM-dd");
			}
		}],
		jsonUrl : rootPath + '/promotion/selectList',
		checkbox : true,
	});
	$("#search").click("click", function() {// 绑定查询按扭
		var searchParams = $("#searchForm").serializeJson();// 初始化传参数
		grid.setOptions({
			data : searchParams
		});
	});
	$("#addPro").click("click", function() {
		addPro();
	});
	$("#editPro").click("click", function() {
		editPro();
	});
	$("#delPro").click("click", function() {
		delPro();
	});
});
function addPro() {
	//一系列的校验
	//loadPage(rootPath+"/promotion/add");
	pageii = layer.open({
		title : "新增",
		type : 2,
		area : [ "48%", "50%" ],
		content : rootPath + '/promotion/add'
	});
}
function editPro() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox.length > 1 || cbox == "") {
		layer.msg("只能选中一个");
		return;
	}
	
	//loadPage(rootPath+"/promotion/edit?id="+cbox);
	pageii = layer.open({
		title : "编辑",
		type : 2,
		area : ["48%", "30%" ],
		content : rootPath + '/promotion/edit?id=' + cbox
	});
}
function delPro() {
	var cbox = grid.getSelectedCheckbox();
	if (cbox == "") {
		layer.msg("请选择删除项！！");
		return;
	}
	layer.confirm('是否删除？', function(index) {
		var url = rootPath + '/promotion/del';
		var s = CommnUtil.ajax(url, {
			ids : cbox.join(",")
		}, "json");
		console.log( s);
		if (s == "success") {
			layer.msg('删除成功');
			grid.loadData();
		} else {
			layer.msg('删除失败');
		}
	});
}
